#include "i6d_ucase_common.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int
main(int argc, char *argv[])
{
	struct sockaddr_in6 svaddr, claddr;
	int sfd, j;
	ssize_t numBytes;
	socklen_t len;
	char buf[BUF_SIZE];
	char claddrStr[INET6_ADDRSTRLEN];

	/* socket()*/
	sfd = socket(AF_INET6, SOCK_DGRAM, 0);
	if(sfd == -1)
		errExit("socket() failed");

	/* bind() */
	memset(&svaddr, 0, sizeof(struct sockaddr_in6));
	svaddr.sin6_family = AF_INET6;
	svaddr.sin6_addr = in6addr_any;
	svaddr.sin6_port = htons(PORT_NUM);
	if(bind(sfd, (struct sockaddr *)&svaddr, sizeof(struct sockaddr_in6)-1) == -1)
		errExit("bind() failed");

	/* loop for recvfrom() and sendto() */
	for(;;)
	{
		len = sizeof(struct sockaddr_in6);
		if((numBytes = recvfrom(sfd, buf, BUF_SIZE, 0, &claddr, &len)) == -1)
			errExit("recvfrom() failed");

		if(inet_ntop(AF_INET6, &claddr.sin6_addr, claddrStr, INET6_ADDRSTRLEN) == NULL)
			printf("could not convert client addr to string\n");
		else
			printf("Server received %ld bytes from (%s, %u)\n", (long)numBytes, claddrStr,
					ntohs(claddr.sin6_port));

		for(j = 0; j < numBytes; j ++)
			buf[j] = toupper((unsigned char)buf[j]);

		if(sendto(sfd, buf, numBytes, 0, (struct sockaddr *)&claddr, len) != numBytes)
			fatal("sento failed\n");
	}

	exit(EXIT_SUCCESS);
}
